package com.ymatou.iapi.godmch.testcase;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.CheckPermissionsBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.service.CheckPermissionsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
/**
*校验多项客服权限
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CheckPermissions{
	private static CheckPermissionsBean checkpermissionsBean;
	private static CheckPermissionsCall checkpermissionsCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("校验多项客服权限");
	}

	@Before
	public void caseUp() {
		checkpermissionsBean=new CheckPermissionsBean();
		checkpermissionsCall=new CheckPermissionsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(checkpermissionsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckPermissions_001() {
		Logger.start(true,"校验多项客服权限,成功-销售专员");
		try {
			Integer templateId=1;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
	        int[] permissionCodes = new DefGodMchWapper().selectPermission(templateId).stream().mapToInt(mapper->mapper.getPermissionCode()).toArray();
	        //获取templateid权限列表 因为csid是templateId=1 相对固定
			List<Integer> listpermissionCodes = Arrays.stream(permissionCodes).boxed().collect(Collectors.toList());
			
	       checkpermissionsBean.setPermissionCodes(GodmchManageCallService.ALLPermissionList);
	       checkpermissionsBean.setCsId(csId);
	       checkpermissionsCall.setData(checkpermissionsBean);
	       checkpermissionsCall.callService();
	       Logger.verifyEquals("200", checkpermissionsCall.getString("status"), "验证status");
	       checkpermissionsCall.getCheckPermissionDtos().forEach(pd->{
				Logger.verifyEquals(listpermissionCodes.contains(pd.getPermissionCode()),(pd.getHas()),"验证是否有权限");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckPermissions_002() {
		Logger.start(true,"校验多项客服权限,成功-发货专员");
		try {
			Integer templateId=2;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
	        int[] permissionCodes = new DefGodMchWapper().selectPermission(templateId).stream().mapToInt(mapper->mapper.getPermissionCode()).toArray();
	        //获取templateid权限列表 因为csid是templateId=1 相对固定
			List<Integer> listpermissionCodes = Arrays.stream(permissionCodes).boxed().collect(Collectors.toList());
			
	       checkpermissionsBean.setPermissionCodes(GodmchManageCallService.ALLPermissionList);
	       checkpermissionsBean.setCsId(csId);
	       checkpermissionsCall.setData(checkpermissionsBean);
	       checkpermissionsCall.callService();
	       Logger.verifyEquals("200", checkpermissionsCall.getString("status"), "验证status");
	       checkpermissionsCall.getCheckPermissionDtos().forEach(pd->{
				Logger.verifyEquals(listpermissionCodes.contains(pd.getPermissionCode()),(pd.getHas()),"验证是否有权限");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckPermissions_003() {
		Logger.start(true,"校验多项客服权限,成功-普通运营");
		try {
			Integer templateId=3;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
	        int[] permissionCodes = new DefGodMchWapper().selectPermission(templateId).stream().mapToInt(mapper->mapper.getPermissionCode()).toArray();
	        //获取templateid权限列表 因为csid是templateId=1 相对固定
			List<Integer> listpermissionCodes = Arrays.stream(permissionCodes).boxed().collect(Collectors.toList());
			
	       checkpermissionsBean.setPermissionCodes(GodmchManageCallService.ALLPermissionList);
	       checkpermissionsBean.setCsId(csId);
	       checkpermissionsCall.setData(checkpermissionsBean);
	       checkpermissionsCall.callService();
	       Logger.verifyEquals("200", checkpermissionsCall.getString("status"), "验证status");
	       checkpermissionsCall.getCheckPermissionDtos().forEach(pd->{
				Logger.verifyEquals(listpermissionCodes.contains(pd.getPermissionCode()),(pd.getHas()),"验证是否有权限");
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckPermissions_004() {
		Logger.start(true,"校验多项客服权限,成功-高级运营");
		try {
			Integer templateId=4;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
	        int[] permissionCodes = new DefGodMchWapper().selectPermission(templateId).stream().mapToInt(mapper->mapper.getPermissionCode()).toArray();
	        //获取templateid权限列表 因为csid是templateId=1 相对固定
			List<Integer> listpermissionCodes = Arrays.stream(permissionCodes).boxed().collect(Collectors.toList());
			
	       checkpermissionsBean.setPermissionCodes(GodmchManageCallService.ALLPermissionList);
	       checkpermissionsBean.setCsId(csId);
	       checkpermissionsCall.setData(checkpermissionsBean);
	       checkpermissionsCall.callService();
	       Logger.verifyEquals("200", checkpermissionsCall.getString("status"), "验证status");
	       checkpermissionsCall.getCheckPermissionDtos().forEach(pd->{
				Logger.verifyEquals(listpermissionCodes.contains(pd.getPermissionCode()),(pd.getHas()),"验证是否有权限");
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckPermissions_005() {
		Logger.start(true,"校验多项客服权限,不输入csid失败");
		try {
			checkpermissionsBean.setPermissionCodes(GodmchManageCallService.ALLPermissionList);
			checkpermissionsCall.setData(checkpermissionsBean);
			checkpermissionsCall.callService();			
			Logger.verifyEquals("4001", checkpermissionsCall.getString("status"), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckPermissions_006() {
		Logger.start(true,"校验多项客服权限,csid已移除没有权限");
		try {
			Integer templateId=4;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
	        GodmchManageCallService.RemoveCs(mchId, csId);
	        Logger.comment("移除客服:"+csId);
	        int[] permissionCodes = new DefGodMchWapper().selectPermission(templateId).stream().mapToInt(mapper->mapper.getPermissionCode()).toArray();
	        //获取templateid权限列表 因为csid是templateId=1 相对固定
			List<Integer> listpermissionCodes = Arrays.stream(permissionCodes).boxed().collect(Collectors.toList());
			
	       checkpermissionsBean.setPermissionCodes(GodmchManageCallService.ALLPermissionList);
	       checkpermissionsBean.setCsId(csId);
	       checkpermissionsCall.setData(checkpermissionsBean);
	       checkpermissionsCall.callService();
	       Logger.verifyEquals("5000", checkpermissionsCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckPermissions_007() {
		Logger.start(true,"校验多项客服权限,不输入permissionCodes失败");
		try {
			Integer templateId=4;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
			checkpermissionsBean.setCsId(csId);
			checkpermissionsCall.setData(checkpermissionsBean);
			checkpermissionsCall.callService();			
			Logger.verifyEquals("4001", checkpermissionsCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}